import { _decorator, Component, Enum, EventTouch, find, instantiate, Node, Prefab } from 'cc';
import { PROPS } from '../AshFramework/Datas/Constant';
import { PrefsManager } from '../AshFramework/Managers/PrefsManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { GameManager } from './GameManager';
import { HintController } from './HintController';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { GameData } from './GameData';
import { Shop } from './Shop';
import { TLWLSJ_UIManager } from './UIManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('PropsItem')
export class PropsItem extends Component {

    @property({ type: Enum(PROPS) })
    Type: PROPS = PROPS.药丸;

    @property
    Price: number = 0;

    private _propName: string = "";

    protected onLoad(): void {
        find("购买", this.node).on(Node.EventType.TOUCH_END, this.onBuy, this);
        this._propName = ZTool.GetEnumKeyByValue(PROPS, this.Type);
    }


    onBuy(event: EventTouch) {
        AudioManager.PlaySound(Audios.ButtonClick);
        if (PrefsManager.Instance.userData.Gold >= this.Price) {
            Shop.Instance.showGold(this.Price);
            PrefsManager.Instance.saveData();
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                GameData.addPropByType(this.Type);
                const hint: string = `购买成功！${this._propName}:${GameData.getPropByType(this.Type)}`;
                node.getComponent(HintController).showHint(hint);
                if (this.Type == PROPS.药丸) {
                    TLWLSJ_UIManager.Instance.showYW(1);
                } else if (this.Type == PROPS.咖啡) {
                    TLWLSJ_UIManager.Instance.showKF(1);
                }
            })
        } else {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                const hint: string = `余额不足！`;
                node.getComponent(HintController).showHint(hint);
            })
        }
    }
}


